﻿// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using SharpDX.Mathematics;

namespace SharpDX.Toolkit.Graphics
{
    public class ModelMesh : ComponentBase
    {
        public ModelBone ParentBone;

        public BoundingSphere BoundingSphere;

        internal VertexBufferBindingCollection VertexBuffers;

        internal BufferCollection IndexBuffers;

        public ModelMeshPartCollection MeshParts;

        public PropertyCollection Properties;

        public ModelMesh()
        {
            Effects = new ModelEffectCollection();
        }

        public ModelEffectCollection Effects { get; private set; }

        /// <summary>
        /// Iterator on each <see cref="ModelMeshPart"/>.
        /// </summary>
        /// <param name="meshPartFunction">The mesh part function.</param>
        public void ForEach(Action<ModelMeshPart> meshPartFunction)
        {
            int meshPartCount = MeshParts.Count;
            for (int i = 0; i < meshPartCount; i++)
            {
                meshPartFunction(MeshParts[i]);
            }
        }

        /// <summary>
        /// Draws all of the ModelMeshPart objects in this mesh, using their current Effect settings.
        /// </summary>
        /// <param name="context">The graphics context.</param>
        /// <param name="boneTransforms">The model's bone transforms.</param>
        /// <param name="effectOverride">The effect to use instead of the effect attached to each mesh part. Default is null (use Effect in MeshPart)</param>
        /// <exception cref="System.InvalidOperationException">Model has no effect</exception>
        public void Draw(GraphicsDevice context, Matrix[] boneTransforms, Effect effectOverride = null)
        {
            int count = this.MeshParts.Count;
            for (int i = 0; i < count; i++)
            {
                ModelMeshPart part = MeshParts[i];
                Effect effect = effectOverride ?? part.Effect;
                if (effect == null)
                {
                    throw new InvalidOperationException("ModelMeshPart has no effect and effectOverride is null");
                }

                var skinnedEffect = effect as SkinnedEffect;
                int boneCount = part.SkinnedBones.Count;
                if (skinnedEffect != null && boneCount > 0)
                {
                    var transforms = new Matrix[boneCount];
                    for (int j = 0; j < boneCount; j++)
                    {
                        var skinnedBone = part.SkinnedBones[j];
                        Matrix.Multiply(ref skinnedBone.OffsetMatrix, ref boneTransforms[skinnedBone.Bone.Index], out transforms[j]);
                    }

                    skinnedEffect.SetBoneTransforms(transforms);
                }

                int passCount = effect.CurrentTechnique.Passes.Count;
                for (int j = 0; j < passCount; j++)
                {
                    effect.CurrentTechnique.Passes[j].Apply();
                    part.Draw(context);
                }
            }
        }

    }
}